home *** CD-ROM | disk | FTP | other *** search
- /* Fchart - fmisc.c */
- /*
- * Gnuplot code
- * Copyright (C) 1986, 1987, 1990 Thomas Williams, Colin Kelley
- *
- * Permission to use, copy, and distribute this software and its
- * documentation for any purpose with or without fee is hereby granted,
- * provided that the above copyright notice appear in all copies and
- * that both that copyright notice and this permission notice appear
- * in supporting documentation.
- *
- * Permission to modify the software is granted, but not the right to
- * distribute the modified code. Modifications are to be distributed
- * as patches to released version.
- *
- * This software is provided "as is" without express or implied warranty.
- *
- *
- * AUTHORS
- *
- * Original Software:
- * Thomas Williams, Colin Kelley.
- *
- * Gnuplot 2.0 additions:
- * Russell Lang, Dave Kotz, John Campbell.
- *
- * Fchart changes and additions:
- * Piotr Filip Sawicki
- *
- * send your comments or suggestions to fs@uwasa.fi
- *
- */
-
- #include <stdio.h>
- #include "plot.h"
- #include "fchart.h"
- #include "help.h"
-
- #ifdef __TURBOC__
- #include <graphics.h>
- #endif
-
- extern BOOLEAN autoscale;
- extern BOOLEAN auto_label;
- extern BOOLEAN log_y;
- extern BOOLEAN b_clockwise, p_clockwise;
- extern BOOLEAN draw_border;
- extern FILE* outfile;
- extern char outstr[];
- extern int samples;
- extern int term;
- extern double zero;
- extern double base;
- extern double radexp;
- extern double b_wid, b_spc, b_int;
- extern float xsize, ysize;
- extern double roff, loff, toff, boff;
- extern enum GRAV_DIR gravity, explode;
- extern enum INP_STYLE inp_style;
- extern enum DRAW_STYLE data_style;
- extern enum FONT_STYLE vect_font;
- extern struct pair data_place, label_plac;
- extern int HLitem;
- extern struct dfile data_head;
- extern int xmin, xmax;
- extern double treshold;
- extern char thrname[];
- extern char tic_form[];
- extern int strunc;
-
- extern BOOLEAN screen_ok;
-
- extern int c_token, num_tokens;
- extern struct termentry term_tbl[];
-
- extern char *sprintf(),*strcpy();
- static char *grav_name();
-
- extern void show_labels(), show_arrow();
-
- char *alloc(size, message)
- unsigned size;
- char *message;
- {
- char *malloc();
- char *p; /* the new allocation */
- char errbuf[100]; /* error message string */
- extern char *malloc();
-
- p = malloc(size);
- if (p == (char *)NULL) {
-
- #ifndef VMS
- FreeHelp(); /* out of memory, try to make some room */
- #endif
-
- p = malloc(size); /* try again */
- if (p == (char *)NULL) {
- /* really out of memory */
- if (message != NULL) {
- (void) sprintf(errbuf, "out of memory for %s", message);
- int_error(errbuf, NO_CARET);
- /* NOTREACHED */
- }
- /* else we return NULL */
- }
- }
-
- return(p);
- }
-
- destroy(p) /* destroy dfile structure p and all the following */
- struct dfile *p;
- {
- struct dfile *t;
- struct chunk *a, *b;
- int i;
-
- while (p) {
- for (a=p->data; a; ) {
- free((char *)a->dval);
- if (a->vlbl) {
- for (i=0; i<a->used; i++)
- if (a->vlbl[i])
- free(a->vlbl[i]);
- free((char *)a->vlbl);
- }
- b = a;
- a = a->next;
- free((char *)b);
- }
- if (p->fname) free(p->fname);
- t = p;
- p = p->dnxt;
- free((char *)t);
- }
- }
-
- load_file(fp)
- FILE *fp;
- {
- register int len;
- extern char input_line[];
- int start, left, more, stop = FALSE;
-
- if (!fp)
- os_error("Cannot open load file",c_token);
-
- while (!stop) { /* read all commands in file */
- /* read one command */
- left = MAX_LINE_LEN;
- start = 0;
- more = TRUE;
-
- while (more) {
- if (fgets(&(input_line[start]), left, fp) == NULL) {
- stop = TRUE; /* EOF in file */
- input_line[start] = '\0';
- more = FALSE;
- } else {
- len = strlen(input_line) - 1;
- if (input_line[len] == '\n') { /* remove any newline */
- input_line[len] = '\0';
- /* Look, len was 1-1 = 0 before, take care here! */
- if (len > 0) --len;
- } else if (len+1 >= left)
- int_error("Input line too long",NO_CARET);
-
- if (input_line[len] == '\\') { /* line continuation */
- start = len;
- left -= len;
- } else
- more = FALSE;
- }
- }
-
- if (strlen(input_line) > 0) {
- screen_ok = FALSE; /* make sure command line is
- echoed on error */
- do_line();
- }
- }
- }
-
- save_sets(fp) /* save all current settings */
- FILE *fp;
- {
- char comm[MAX_LINE_LEN+1];
-
- if (!fp)
- os_error("Cannot open save file",c_token);
-
- for(c_token++; !END_OF_COMMAND; c_token++) {
- if (equals(c_token,",")) continue;
- if (!isstring(c_token)) break; /* causes error, but later */
- quote_str(comm, c_token);
- fprintf(fp,"# %s\n",comm);
- }
-
- (void) putc('\n',fp);
- fprintf(fp,"set %sautolabel\n", auto_label ? "" : "no");
- fprintf(fp,"set %struncate", strunc==-1 ? "no" : "");
- if (strunc>0)
- fprintf(fp," %d", strunc);
- (void) putc('\n',fp);
- fprintf(fp,"set cust ");
- if (data_place.from != -1) {
- show_segment(data_place.from,data_place.upto,fp);
- (void) putc(' ',fp);
- if (label_plac.from != -1)
- show_segment(label_plac.from,label_plac.upto,fp);
- }
- (void) putc('\n',fp);
- fprintf(fp,"set font ");
- switch (vect_font) {
- case F_NEVER: {
- fprintf(fp,"never\n");
- break;
- }
- case F_WHENN: {
- fprintf(fp,"when_needed\n");
- break;
- }
- case F_ROTAT: {
- fprintf(fp,"rotated\n");
- break;
- }
- case F_ALWYS: {
- fprintf(fp,"always\n");
- break;
- }
- }
- fprintf(fp,"set format \"%s\"\n", tic_form);
- fprintf(fp,"set %sframe\n", draw_border ? "" : "no");
- fprintf(fp,"set highlight ");
- switch (HLitem) {
- case HL_MIN: {
- fprintf(fp,"min\n");
- break;
- }
- case HL_MAX: {
- fprintf(fp,"max\n");
- break;
- }
- case HL_NON: {
- fprintf(fp,"none\n");
- break;
- }
- default: {
- fprintf(fp,"%d\n",HLitem);
- break;
- }
- }
- fprintf(fp,"set input ");
- switch (inp_style) {
- case GNUPLOT: {
- fprintf(fp,"gnuplot\n");
- break;
- }
- case PRIVATE: {
- fprintf(fp,"private\n");
- break;
- }
- case CUSTOMD: {
- fprintf(fp,"customized\n");
- break;
- }
- }
- fprintf(fp,"set %slogscale\n", log_y ? "" : "no");
- fprintf(fp,"set offsets %lg, %lg, %lg, %lg\n", loff, roff, toff, boff);
- fprintf(fp,"set output %s\n", strcmp(outstr,"STDOUT") ? outstr : "");
- fprintf(fp,"set size %g, %g\n", xsize, ysize);
- fprintf(fp,"set style ");
- switch (data_style) {
- case ABARS: fprintf(fp,"adj\n"); break;
- case SBAR: fprintf(fp,"sta\n"); break;
- case LAYB: fprintf(fp,"lay\n"); break;
- case PIECHART: fprintf(fp,"pie\n"); break;
- }
- fprintf(fp,"set term %s\n", term_tbl[term].name);
- fprintf(fp,"set title ");
- if (data_head.fname) fprintf(fp,"\"%s\"\n", data_head.fname);
- else (void) putc('\n',fp);
- show_labels(0, fp, TRUE);
- show_arrow(0, fp, TRUE);
- fprintf(fp,"set range ");
- if (xmin != -1) show_segment(xmin,xmax,fp);
- (void) putc('\n',fp);
- fprintf(fp,"set zero %lg\n", zero);
-
- (void) putc('\n',fp);
-
- fprintf(fp,"set bar %sautoscale\n", autoscale ? "" : "no");
- fprintf(fp,"set bar base %lg\n", base);
- fprintf(fp,"set bar %sclock\n", b_clockwise ? "" : "counter_");
- fprintf(fp,"set bar grav %s\n", grav_name(gravity));
- fprintf(fp,"set bar width %lg, %lg, %lg\n", b_wid, b_int, b_spc);
-
- (void) putc('\n',fp);
-
- fprintf(fp,"set pie expl %s\n", grav_name(explode));
- fprintf(fp,"set pie %sclockwise\n", p_clockwise ? "" : "counter_");
- fprintf(fp,"set pie radius %lg\n", radexp);
- fprintf(fp,"set pie samples %d\n", samples);
- fprintf(fp,"set pie threshold %lg \"%s\"\n", treshold, thrname);
-
- (void) fclose(fp);
- }
-
- int instring(str, c)
- char *str;
- char c;
- {
- int pos = 0;
-
- while (str != NULL && *str != '\0' && c != *str) {
- str++;
- pos++;
- }
- return (pos);
- }
-
- show_style()
- {
- fprintf(stderr,"\tdata are plotted with ");
- switch (data_style) {
- case ABARS: fprintf(stderr,"adjacent bars\n"); break;
- case LAYB: fprintf(stderr,"layer bars\n"); break;
- case SBAR: fprintf(stderr,"stacked bars\n"); break;
- case PIECHART: fprintf(stderr,"piechart\n"); break;
- }
- }
-
- show_range()
- {
- if (xmin==-1)
- fprintf(stderr,"\twhole file is processed");
- else {
- fprintf(stderr,"\tdata range is: ");
- show_segment(xmin,xmax,stderr);
- }
- (void) putc('\n',stderr);
- }
-
- show_zero()
- {
- fprintf(stderr,"\tzero/epsilon is %lg\n",zero);
- }
-
- show_offsets()
- {
- fprintf(stderr,"\toffsets are %lg, %lg, %lg, %lg\n",loff,roff,toff,boff);
- }
-
- show_samples()
- {
- fprintf(stderr,"\tsampling rate for piechart is %d\n",samples);
- }
-
- show_output()
- {
- fprintf(stderr,"\toutput is sent to %s\n",outstr);
- }
-
- show_term()
- {
- fprintf(stderr,"\tterminal type is %s\n",term_tbl[term].name);
- }
-
- show_autoscale()
- {
- fprintf(stderr,"\tbar autoscaling is %s\n",(autoscale)? "ON" : "OFF");
- }
-
- show_logscale()
- {
- if (log_y)
- fprintf(stderr,"\tlogscaling is ON\n");
- else
- fprintf(stderr,"\tno logscaling\n");
- }
-
- show_version()
- {
- extern char version[];
- extern char date[];
- static char *authors[] = {"Thomas Williams","Colin Kelley"};
- extern int patchlevel;
- extern char bug_email[];
- int x;
- long time();
-
- x = time((long *)NULL) & 1;
- fprintf(stderr,"\n\t%s\n\t%sversion %s patchlevel %d\n\tlast modified %s\n",
- PROGRAM, OS, version, patchlevel, date);
- fprintf(stderr,"\tGnuplot v.1.x Copyright (C) 1986, 1987 %s, %s\n",
- authors[x],authors[1-x]);
- fprintf(stderr,"\tGnuplot v.2.0 John Campbell, David Kotz, Russell Lang\n");
- fprintf(stderr,"\t%s Copyright (C) 1990 Piotr Filip Sawicki\n",PROGRAM);
- #ifdef __TURBOC__
- fprintf(stderr,"\tCreated using Turbo C, Copyright Borland 1987, 1988\n");
- #endif
- fprintf(stderr, "\n\tSend bugs and comments to %s\n\n", bug_email);
- }
-
- static char *grav_name(what)
- enum GRAV_DIR what;
- {
- static char buf[20];
- switch (what) {
- case(SOUTH) : (void) strcpy(buf,"bottom"); break;
- case(NORTH) : (void) strcpy(buf,"top"); break;
- case(EAST) : (void) strcpy(buf,"right"); break;
- case(WEST) : (void) strcpy(buf,"left"); break;
- case(DEFAULT) : buf[0] = '\0'; break;
- }
- return(buf);
- }
-
- show_gravity()
- {
- fprintf(stderr,"\tbar gravitation to the %s\n", grav_name(gravity));
- }
-
- show_explode()
- {
- fprintf(stderr,"\thighlited slice explodes %s\n",
- explode == DEFAULT
- ? "where it should"
- : grav_name(explode));
- }
-
- show_HLset()
- {
- switch (HLitem) {
- case(HL_NON): fprintf(stderr,"\tno item"); break;
- case(HL_MIN): fprintf(stderr,"\tminimal item"); break;
- case(HL_MAX): fprintf(stderr,"\tmaximal item"); break;
- default : fprintf(stderr,"\titem no. %d",HLitem); break;
- }
- fprintf(stderr," is highlited\n");
- }
-
- show_base()
- {
- fprintf(stderr,"\tbase for bars is %lg\n",base);
- }
-
- show_input()
- {
- switch (inp_style) {
- case(GNUPLOT) : fprintf(stderr,"\tgnuplot-compatible"); break;
- case(PRIVATE) : fprintf(stderr,"\tstandard"); break;
- case(CUSTOMD) : fprintf(stderr,"\tcustomized"); break;
- }
- fprintf(stderr," data input style\n");
- }
-
- show_custom()
- {
- if (data_place.from == -1)
- fprintf(stderr,"\tcustomized input format not given\n");
- else {
- fprintf(stderr,"\tcustomized input format:\n");
- fprintf(stderr,"\t\tvalue: ");
- show_segment(data_place.from,data_place.upto,stderr);
- if (label_plac.from == -1) fprintf(stderr,"\n\t\tno label position");
- else {
- fprintf(stderr,"\n\t\tlabel: ");
- show_segment(label_plac.from,label_plac.upto,stderr);
- }
- (void) putc('\n',stderr);
- }
- }
-
- static show_segment(l,r,fp)
- int l,r;
- FILE *fp;
- {
- (void) putc('[',fp);
- if (l) fprintf(fp,"%d",l);
- fprintf(fp," : ");
- if (r>-1) fprintf(fp,"%d",r);
- (void) putc(']',fp);
- }
-
- show_label()
- {
- fprintf(stderr,"\tlabels are%s auto-generated\n",
- auto_label ? "" : "n't");
- }
-
- show_clock(pie)
- BOOLEAN pie;
- {
- if (pie)
- fprintf(stderr,"\tpies are drawn %sclockwise\n",
- p_clockwise ? "" : "counter-");
- else
- fprintf(stderr,"\tbars are drawn %sclockwise\n",
- b_clockwise ? "" : "counter-");
- }
-
- show_width()
- {
- fprintf(stderr,"\tbar width params are %lg, %lg, %lg\n",b_wid,b_int,b_spc);
- }
-
- show_radius()
- {
- fprintf(stderr,"\texplosion radius ratio is %lg\n",radexp);
- }
-
- show_border()
- {
- fprintf(stderr,"\tpicture frame is%s drawn\n",draw_border?"":" not");
- }
-
- show_size()
- {
- fprintf(stderr,"\tsize is scaled by %g, %g\n",xsize,ysize);
- }
-
- show_title()
- {
- if (!data_head.fname || !*(data_head.fname))
- fprintf(stderr,"\tpicture has no title\n");
- else
- fprintf(stderr,"\ttitle for picture is: \"%s\"\n",data_head.fname);
- }
-
- show_tresh()
- {
- fprintf(stderr,"\tslices glue threshold is %lg with name \"%s\"\n", treshold, thrname);
- }
-
- show_format()
- {
- fprintf(stderr,"\tformat for numbers is: \"%s\"\n", tic_form);
- }
-
- show_font()
- {
- fprintf(stderr,"\tvector font is ");
- switch (vect_font) {
- case F_NEVER: fprintf(stderr,"never used\n"); break;
- case F_WHENN: fprintf(stderr,"used when needed\n"); break;
- case F_ROTAT: fprintf(stderr,"for rotated text\n"); break;
- case F_ALWYS: fprintf(stderr,"always used\n"); break;
- }
- }
-
- show_trunc()
- {
- fprintf(stderr,"\tlabels are%s truncated", strunc==-1 ? " not" : "");
- if (strunc>0)
- fprintf(stderr," at char no. %d", strunc);
- fprintf(stderr,"\n");
- }
-